package com.glodon.paas.activity.client;

import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class DemoServiceClient {

    String activityApiUrl = "http://127.0.0.1:8080/activity/api/internal/activityDemo";

    public String getDemo1() {
        WebResource resource = Client.create().resource(activityApiUrl + "/demo/getDemo1");
        ClientResponse client = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
        System.out.println(client.getStatus());
        System.out.println(resource);

        if (client.getStatus() == 204) {
            // 成功，返回null
            return null;
        }
        if (client.getStatus() != 200) {
            // 异常
            return "error";
        }

        return client.getEntity(String.class);
    }

    public String getDemo2() {
        WebResource resource = buileResource();
        return resource.path("demo").path("path").accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
    }

    public String postDemo1() {
        WebResource resource = Client.create().resource(activityApiUrl + "/demo/postDemo1");
        ClientResponse client = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, "postData");
        System.out.println(client.getStatus());
        if (client.getStatus() == 204) {
            // 成功，返回null
            return null;
        }
        if (client.getStatus() != 200) {
            // 异常
            return "error";
        }

        return client.getEntity(String.class);
    }

    public String postDemo2() {
        WebResource resource = buileResource();
        Builder builder = resource.path("demo").path("postDemo2").accept(MediaType.APPLICATION_JSON_TYPE);
        ClientResponse client = builder.post(ClientResponse.class, "postData");
        System.out.println(client.getStatus());
        if (client.getStatus() == 204) {
            // 成功，返回null
            return null;
        }
        if (client.getStatus() != 200) {
            // 异常
            return "error";
        }

        return client.getEntity(String.class);
    }

    public String postPublish() {
        WebResource resource = buileResource();
        Builder builder = resource.path("publish").accept(MediaType.APPLICATION_JSON_TYPE);
        ClientResponse client = builder.post(ClientResponse.class, "postData");
        System.out.println(client.getStatus());
        if (client.getStatus() == 204) {
            // 成功，返回null
            return null;
        }
        if (client.getStatus() != 200) {
            // 异常
            return "error";
        }

        return client.getEntity(String.class);
    }

    public WebResource buileResource() {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource webResource = client.resource(activityApiUrl);
        return webResource;
    }

    public static void main(String[] args) {
        DemoServiceClient client = new DemoServiceClient();
        System.out.println("getDemo1()" + client.getDemo1());
        System.out.println("getDemo2()" + client.getDemo2());
        System.out.println("postDemo1()" + client.postDemo1());
        System.out.println("postDemo2()" + client.postDemo2());
        System.out.println("postPublish()" + client.postPublish());
    }
}
